//
//  RBMineVC.m
//  zhongzhenglu
//
//  Created by XL on 2022/5/26.
//

#import "RBMineVC.h"
#import "RBDefine.h"
#import "UIView+RBAdd.h"
#import "UIScrollView+RBAdd.h"
#import <Masonry.h>
#import "RBThemeConfig.h"
#import <UIColor+YYAdd.h>
#import <UIView+YYAdd.h>
#import "RanTool.h"
#import "RBPager.h"
#import <UINavigationController+FDFullscreenPopGesture.h>
#import "UIViewController+RBAdd.h"
#import "RBSettingVC.h"
#import "RBMyGroupVC.h"
#import "RBMyCollectVC.h"
#import "RBDaylySignVC.h"
#import "RBHistoryVC.h"
#import "RBContactServiceVC.h"
#import "RBApplyInvoiceVC.h"
#import "RBInviteGiftVC.h"
#import "RBMyWalletVC.h"
#import "RBMyCoinVC.h"
#import "RBMyCouponVC.h"
#import "RBStockOrderVC.h"
#import "RBGoodsOrderVC.h"
#import "RBProcessOrderVC.h"
#import "RBServiceOrderVC.h"
#import "UIViewController+RBAdd.h"
#import "RBMyMembersVC.h"
#import "RBAppDelegateHelper.h"
#import "RBMineResponse.h"
#import "RBIntegralDetailVC.h"

@interface RBMineVC ()

@property(nonatomic, strong)UIScrollView *scrollView;

@property(nonatomic, strong)IBOutlet UIView *infoView;
@property(nonatomic, strong)IBOutlet UIImageView *iconImgView;
@property(nonatomic, strong)IBOutlet UILabel *nameL;
@property(nonatomic, strong)IBOutlet UILabel *telL;
//余额
@property (weak, nonatomic) IBOutlet UILabel *balanceLabel;
//虚拟币
@property (weak, nonatomic) IBOutlet UILabel *virtualCoinLabel;
//积分
@property (weak, nonatomic) IBOutlet UILabel *intergralLabel;
//优惠券
@property (weak, nonatomic) IBOutlet UILabel *couponLabel;
//库存
@property (weak, nonatomic) IBOutlet UILabel *kucunLabel;

//itemView:tag=3是numL

@property(nonatomic, strong)IBOutlet UIView *orderView;
@property(nonatomic, strong)RBPagerHeaderView *orderHeaderView;

@property(nonatomic, strong)IBOutlet UIView *serviceView;

@property (nonatomic, strong) RBMineResponse *response;


@end

@implementation RBMineVC

#pragma mark 页面已显示
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    [self getData];
}
#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
}

#pragma mark - Private

- (void)setup
{
    self.statusBarStyle = UIStatusBarStyleLightContent;
    self.fd_prefersNavigationBarHidden = YES;
    
    [self.scrollView rbSetContentWidth:kRBMainScreenWidth];

    [self.infoView zoomSubViewsWithSelf];
    self.infoView.height = Zoom(340) + kRBStatusBarHeight;
    [self.scrollView addView:self.infoView withHeight:self.infoView.height width:kRBMainScreenWidth top:0 bottom:0 left:0 right:0 centerX:0];
    
    [self.orderView zoomSubViewsWithSelf];
    self.orderView.height = Zoom(self.orderView.height);
    self.orderView.width = Zoom(self.orderView.width);
    [self.orderView addSubview:self.orderHeaderView];
    [self.scrollView addView:self.orderView withHeight:self.orderView.height width:self.orderView.width top:Zoom(10) bottom:0 left:CGFLOAT_MIN right:CGFLOAT_MIN centerX:YES];
    
    [self.serviceView zoomSubViewsWithSelf];
    self.serviceView.height = Zoom(self.serviceView.height);
    self.serviceView.width = Zoom(self.serviceView.width);
    [self.scrollView addView:self.serviceView withHeight:self.serviceView.height width:self.serviceView.width top:Zoom(10) bottom:30 left:CGFLOAT_MIN right:CGFLOAT_MIN centerX:YES];
}

#pragma mark - Network

#pragma mark - Event

- (IBAction)settingBtnTapped
{
    RBSettingVC *vc = RBSettingVC.new;
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)myMembersBtnTapped
{
    RBMyMembersVC *vc = RBMyMembersVC.new;
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)numViewTapped:(UIButton *)btn
{
    NSArray *classArray = @[
        RBMyWalletVC.class,
        RBMyCoinVC.class,
        RBMyCouponVC.class,
        RBStockOrderVC.class,
        RBIntegralDetailVC.class,
    ];
    Class vcClass = classArray[btn.tag];
    UIViewController *vc = vcClass.new;
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)allOrderBtnTapped:(id)sender
{
    if(self.orderHeaderView.currentIndex == 0)
    {
        RBGoodsOrderVC *vc = RBGoodsOrderVC.new;
        [self.navigationController pushViewController:vc animated:YES];
    }
    else
    {
        RBProcessOrderVC *vc = RBProcessOrderVC.new;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (IBAction)orderViewTapped:(UIButton *)btn {
    if (btn.tag == 4) {
        RBServiceOrderVC *vc = RBServiceOrderVC.new;
        [self.navigationController pushViewController:vc animated:YES];
    } else {
        if (self.orderHeaderView.currentIndex == 0) {
            RBGoodsOrderVC *vc = RBGoodsOrderVC.new;
            vc.index = btn.tag + 1;
            [self.navigationController pushViewController:vc animated:YES];
        } else {
            RBProcessOrderVC *vc = RBProcessOrderVC.new;
            vc.index = btn.tag + 1;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
}

- (IBAction)serviceViewTapped:(UIButton *)btn
{
    NSArray *classArray = @[
        RBMyGroupVC.class,
        RBMyCollectVC.class,
        RBDaylySignVC.class,
        RBHistoryVC.class,
        RBContactServiceVC.class,
        RBApplyInvoiceVC.class,
        RBInviteGiftVC.class
    ];
    if (btn.tag == 4) {
        [UIApplication LC_openURL_phone:self.response.data.store_tel];
        return;
    }
    Class vcClass = classArray[btn.tag];
    UIViewController *vc = vcClass.new;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark 网络请求
- (void)getData {
    LCWeakSelf;
    [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
        
    } path:_Mine post:YES log:YES responseClass:RBMineResponse.class desView:self.view showHud:!self.response showPlaceHolder:NO sucBlock:^(RBMineResponse *data) {
        weakSelf.response = data;
        [weakSelf fillData];
    } failBlock:^(RBMineResponse *data) {
        [weakSelf.view showTip:data.message completion:nil];
    } reloadBlock:nil];
}
- (void)fillData {
    self.nameL.text = self.response.data.nickname;
    self.telL.text = [NSString stringWithFormat:@"TEL:%@", self.response.data.mobile];
    [self.iconImgView setImageWithUrl:self.response.data.avatar placeHolder:@"b0.jpg"];
    //余额
    self.balanceLabel.text = self.response.data.balance;
    self.balanceLabel.adjustsFontSizeToFitWidth = YES;
    //虚拟币
    self.virtualCoinLabel.text = self.response.data.virtual_coin;
    self.virtualCoinLabel.adjustsFontSizeToFitWidth = YES;
    //积分
    self.intergralLabel.text = self.response.data.integral;
    self.intergralLabel.adjustsFontSizeToFitWidth = YES;
    //优惠券
    self.couponLabel.text = self.response.data.coupon_count;
    self.couponLabel.adjustsFontSizeToFitWidth = YES;
    //库存
    self.kucunLabel.text = self.response.data.orders_count;
    self.kucunLabel.adjustsFontSizeToFitWidth = YES;
}


#pragma mark - Setter

#pragma mark - Getter

- (UIScrollView *)scrollView
{
    if (!_scrollView)
    {
        self.scrollView = ({
            UIScrollView *view = UIScrollView.new;
            view.showsVerticalScrollIndicator = NO;
            view.showsHorizontalScrollIndicator = NO;
            view.backgroundColor = [UIColor colorWithHexString:@"#F3F3F3"];
            view.bounces = NO;
            kRBIgnoreScrollViewInsetsAdjust(view);
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.mas_equalTo(0);
            }];
            view;
        });
    }
    return _scrollView;
}

- (RBPagerHeaderView *)orderHeaderView
{
    if (!_orderHeaderView)
    {
        self.orderHeaderView = ({
            RBPagerHeaderView *view = RBPagerHeaderView.new;
            view.frame = CGRectMake(Zoom(8), Zoom(18), Zoom(200), Zoom(40));
            __weak typeof(self) weakSelf = self;
            view.model = ({
                RBPagerHeaderModel *model = RBPagerHeaderModel.new;
                model.showSeperator = NO;
                model.headerButtonPadding = Zoom(7);
                model.buttonWidthStyle = RBPagerButtonWidthTitleWidthPlusPadding;
                model.titles = @[@"商品订单", @"加工订单"];
                model.headerAttributeTitlesForSelectedState = ({
                    NSMutableArray *arrayM = NSMutableArray.array;
                    for (NSString *title in model.titles)
                    {
                        [arrayM addObject:[RanTool attributeStringMaker:^(NSMutableArray *maker) {
                            [maker addAttributeStringWithFont:[UIFont boldSystemFontOfSize:Zoom(16)] color:[RBThemeConfig textBlackColor] string:title];
                        }]];
                    }
                    arrayM;
                });
                model.headerAttributeTitlesForNormalState = ({
                    NSMutableArray *arrayM = NSMutableArray.array;
                    for (NSString *title in model.titles)
                    {
                        [arrayM addObject:[RanTool attributeStringMaker:^(NSMutableArray *maker) {
                            [maker addAttributeStringWithFont:[UIFont systemFontOfSize:Zoom(15)] color:[RBThemeConfig textBlackColor] string:title];
                        }]];
                    }
                    arrayM;
                });
                model.bottomLineWidthStyle = RBPagerBottomLineWidthConstValue;
                model.bottomLineHeight = Zoom(3.5);
                model.bottomLineWidth = Zoom(23.5);
                model.bottomLineColor = [UIColor colorWithHexString:@"#518CFF"];
                model;
            });
            view.btnEventBlock = ^(NSInteger index, UIButton *btn) {
                [weakSelf.orderHeaderView scrollBottomLineToIndex:index];
            };
            view;
        });
    }
    return _orderHeaderView;
}

@end
